//
//  MHIMMessageTransferHelper.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/6/30.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHIMMessageTransferHelper.h"
#import "MHMessageNotification.h"
#import "MHCircleMember.h"
#import "MHAttributeModel.h"
#import "MHNewMemberMessage.h"
#import "MHCircleUpdateModel.h"
#import "MHRedPacketRecevieModel.h"
#import "MHPushModel.h"
#import "MHCircleHomeModel.h"
@implementation MHIMMessageTransferHelper
+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    static MHIMMessageTransferHelper *instance = nil;
    dispatch_once(&onceToken,^{
        instance = [[super allocWithZone:NULL] init];
        instance.updateCircleIds = [NSMutableDictionary dictionary];
//        instance.releatedTomeUnReadCount = 0;
//        instance.storeRecentArray = [NSMutableArray array];
    });
    return instance;
}

+ (id)allocWithZone:(struct _NSZone *)zone{
    return [self sharedInstance];
}

- (id) transferCustomElemCmd:(CustomElemCmd *) cmd
{
//    self.releatedTomeUnReadCount ++;
    id model;
    if (cmd.customInfo) {
        
       MHIMMessageType type  =  [[cmd.customInfo objectForKey:@"msgType"] intValue];
        
        if (type == MHIMMessageTypeAnser || type == MHIMMessageTypeAsk ||  type == MHIMMessageTypeComment) {
             model = [MHMessageNotification yy_modelWithJSON:cmd.customInfo];
            [self chacheIMMessageWithName:NSStringFromClass([model class]) model:model];
            self.unReadRecentMessage = [self getUnReadMessageCountWith:NSStringFromClass([model class])];
        }else if (type == MHIMMessageTypeAudit)
        {
            model = [MHMessageNotification yy_modelWithJSON:cmd.customInfo];
            [self chacheIMMessageWithName:NSStringFromClass([model class]) model:model];
            self.unReadAuditMessage = [self getUnReadMessageCountWith:NSStringFromClass([MHCircleMember class])];
        } else if (type == MHIMMessageTypePass)
        {
            model = [MHMessageNotification yy_modelWithJSON:cmd.customInfo];
            [self chacheIMMessageWithName:NSStringFromClass([model class]) model:model];
        }else if (type == MHIMMessageTypeAttribute)
        {
            model = [MHAttributeModel yy_modelWithJSON:cmd.customInfo];
            [self chacheIMMessageWithName:NSStringFromClass([model class]) model:model];
            self.unReadAttriMessage = [self getUnReadMessageCountWith:NSStringFromClass([model class])];
        }else if (type == MHIMMessageTypeNewMember)
        {
            model = [MHNewMemberMessage yy_modelWithJSON:cmd.customInfo];
            [self chacheIMMessageWithName:NSStringFromClass([model class]) model:model];
            self.unReadbNewMemberMessage = [self getUnReadMessageCountWith:NSStringFromClass([model class])];
        }else if (type == MHIMMessageTypeCircleUpdate)
        {
           MHCircleUpdateModel *updateModel = [MHCircleUpdateModel yy_modelWithJSON:cmd.customInfo];
            model = updateModel;
//            [self.updateCircleIds setValue:@true forKey:[NSString stringWithFormat:@"%d",updateModel.circleId]];
            [self chacheIMMessageWithName:NSStringFromClass([model class]) model:model];
            [MHNotificationCenter postNotificationName:MHCircleHomeUpdateNotification object:nil];
        }
        else if (type == MHIMMessageTypePaySuccess)
        {
            [MHNotificationCenter postNotificationName:MHRedPacketPaySuccessNotification object:nil];
        }else if (type == MHIMMessageTypeRedPacketReceive)
        {
                        MHRedPacketRecevieModel *updateModel = [MHRedPacketRecevieModel yy_modelWithJSON:cmd.customInfo];
//                        model = updateModel;
            //            //            [self.updateCircleIds setValue:@true forKey:[NSString stringWithFormat:@"%d",updateModel.circleId]];
            //            [self chacheIMMessageWithName:NSStringFromClass([model class]) model:model];
            [MHNotificationCenter postNotificationName:MHRedPacketReceviceNotification object:updateModel];
        }else if (type == MHIMMessageTypeSystemMessage)
        {
            MHPushModel *push = [MHPushModel new];
            [self chacheIMMessageWithName:NSStringFromClass([push class]) model:push];
            [MHNotificationCenter postNotificationName:MHPushTextNotification object:@"您有新消息了~"];
             self.unReadbSystemMessage = [self getUnReadMessageCountWith:NSStringFromClass([MHPushModel class])];
        }
        
    }

    return  model;
}


-(void) cacheGlobleImGroupUpdate
{
    YYCache *cache=[YYCache cacheWithName:MHCacheGlobleKey];
    [cache setObject:[AXAppDelegate sharedDelegate].imGroupImMessageUpdate forKey:MHChacheIMGroupLastMessage];
}

-(void) chacheIMMessageWithName:(NSString *)modelName model:(id)model
{
    YYCache *cache=[YYCache cacheWithName:MHCacheGlobleKey];
    
    if ([model isKindOfClass:[MHCircleUpdateModel class]]) {
        MHCircleUpdateModel *updateModel = model;
      NSMutableDictionary *dic =  [self fetchChatIMMessageWithName:modelName];
        
        if (updateModel.publishUid != [MHHTTPService sharedInstance].currentUser.userId) {
            if (dic) {
                [dic setObject:@true forKey:[NSString stringWithFormat:@"%d",updateModel.circleId]];
            }else
            {
                dic = [NSMutableDictionary dictionary];
                [dic setObject:@true forKey:[NSString stringWithFormat:@"%d",updateModel.circleId]];
            }
            [cache setObject:dic forKey:modelName];
        }
        return;
    }else if ([model isKindOfClass:[MHCircleHomeModel class]])
    {
        MHCircleHomeModel *homeModel = model;
        NSMutableDictionary *dic =  [self fetchChatIMMessageWithName:MHChacheIMGroupLastMessage];
        if (dic) {
            [dic setObject:homeModel.msgId forKey:homeModel.circleCode];
        }else
        {
            dic = [NSMutableDictionary dictionary];
            [dic setObject:homeModel.msgId forKey:homeModel.circleCode];
        }
        [cache setObject:dic forKey:modelName];
         return;
    }
    
    NSMutableArray *imMessageArr = [self fetchChatIMMessageWithName:modelName];
    if (imMessageArr) {
        [imMessageArr appendObject:model];
        [cache setObject:imMessageArr forKey:modelName];
    }else
    {
        imMessageArr = [NSMutableArray array];
        [imMessageArr appendObject:model];
        [cache setObject:imMessageArr forKey:modelName];
    }
}


-(NSMutableArray *) fetchChatIMMessageWithName:(NSString *) modelName
{
    YYCache *cache=[YYCache cacheWithName:MHCacheGlobleKey];
    return [cache objectForKey:modelName];
}

-(int) getUnReadMessageCountWith:(NSString *) modelName
{
    NSMutableArray *arr =  [self fetchChatIMMessageWithName:modelName];
    return arr.count;
}

-(int)unReadbNewMemberMessage
{
     return [self getUnReadMessageCountWith:NSStringFromClass([MHNewMemberMessage class])];
}

-(int)unReadRecentMessage
{
//    _unReadRecentMessage = unReadRecentMessage;
    return [self getUnReadMessageCountWith:NSStringFromClass([MHMessageNotification class])];
}

-(int)unReadAttriMessage
{
    //    _unReadRecentMessage = unReadRecentMessage;
    return [self getUnReadMessageCountWith:NSStringFromClass([MHAttributeModel class])];
}

-(int)unReadAuditMessage
{
    return [self getUnReadMessageCountWith:NSStringFromClass([MHCircleMember class])];
}
//-(void)setReleatedTomeUnReadCount:(int)releatedTomeUnReadCount
//{
//    _releatedTomeUnReadCount = releatedTomeUnReadCount;O
//    [self.storeRecentArray removeAllObjects];
//}

-(void) resetAllreadWithKey:(NSString *) key
{
//    IMAConversationManager *mgr = [IMAPlatform sharedInstance].conversationMgr;
//    self.releatedTomeUnReadCount = 0;
    
    YYCache *cache=[YYCache cacheWithName:MHCacheGlobleKey];
     [cache removeObjectForKey:key];
    if ([key isEqualToString:NSStringFromClass([MHMessageNotification class])]) {
        self.unReadRecentMessage = 0;
    }else if ([key isEqualToString:NSStringFromClass([MHCircleMember class])])
    {
        self.unReadAuditMessage = 0;
    }else if ([key isEqualToString:NSStringFromClass([MHAttributeModel class])])
    {
        self.unReadAttriMessage = 0;
    }else if ([key isEqualToString:NSStringFromClass([MHNewMemberMessage class])])
    {
        self.unReadbNewMemberMessage = 0;
    }else if ([key isEqualToString:NSStringFromClass([MHPushModel class])])
    {
        self.unReadbSystemMessage = 0;
    }
    
//    [[AXAppDelegate sharedDelegate].manageCoversation setReadMessage:nil succ:nil fail:nil];
//    IMAConversation *temp = [[IMAConversation alloc] initWith:[AXAppDelegate sharedDelegate].manageCoversation];
//    [AXAppDelegate sharedDelegate].manageCoversation
}

@end
